// pages/esp32.js
var app = getApp();
Page({

  data: {
    toothProblemsList:{"red":'https://636c-cloud1-9g2a0ajx3bbad7ff-1325147117.tcb.qcloud.la/img/%E7%BA%A2%E6%A1%86.png?sign=dbc4078d9d67b7d5268bc6196fc70b9f&t=1714896765',"green":'https://636c-cloud1-9g2a0ajx3bbad7ff-1325147117.tcb.qcloud.la/img/%E7%BB%BF%E6%A1%86.png?sign=7c651c914746cc68be465d3d2b5f0824&t=1714896956','red2':'https://636c-cloud1-9g2a0ajx3bbad7ff-1325147117.tcb.qcloud.la/img/%E7%BA%A2%E6%B3%A1%E6%A1%86.png?sign=1366506bdadecea3814a214a709cab94&t=1714896986','green2':'https://636c-cloud1-9g2a0ajx3bbad7ff-1325147117.tcb.qcloud.la/img/%E7%BB%BF%E6%B3%A1%E6%A1%86.png?sign=677c131d48206b67d50142f7fa8d2611&t=1714897001','bg':'https://636c-cloud1-9g2a0ajx3bbad7ff-1325147117.tcb.qcloud.la/img/%E7%89%99%E9%BD%BF%E9%97%AE%E9%A2%981%20(1).png?sign=c5d11d74e5c751e5c5dbd8bb0199fe13&t=1714897063','tanhao':'https://636c-cloud1-9g2a0ajx3bbad7ff-1325147117.tcb.qcloud.la/img/%E7%89%99%E9%BD%BF%E5%8F%B9%E5%8F%B7.png?sign=4f847a7b2e55e4711619d4ab0ddfd5a0&t=1714897053'},
    imgUrl:"",
    list:{"opcode":2,"subcode":0,"content":[],"specific":[],"other":"检查完好"},
  },
  onShow() {
    // this.setData({
    //   imgUrl:"https://www.aistomatology.cn/"
    // })
    this.setData({
      imgUrl:app.globalData.videoUrl
    })
  },
  PutImage(){
    var that=this;
    const query=wx.createSelectorQuery();
            query.select('#myCanvas').fields({ node: true, size: true }).exec((res)=>{
              const canvas = res[0].node
              const ctx = canvas.getContext('2d')
              canvas.width=640;
              canvas.height=640;
              const scale=1;
              ctx.scale(scale, scale)
              const num=640;
              ctx.font =  10/scale + 'px Arial'
              let image = canvas.createImage();
              image.src=that.data.imgUrl
              image.onload = function () {
              console.log("图像加载完成")
              ctx.drawImage(image,0, 0,num,num); // 背景图
             };
             setTimeout(function(){
              var temp=that.data.list;
              var lis=[];
              var data=ctx.getImageData(0,0,num,num).data;
              for (var i = 0; i < data.length; i += 4) {
               var r = data[i];
               var g = data[i + 1];
               var b = data[i + 2];
               var mid=[r,g,b];
               lis.push(mid);
               }
              temp.content=lis;
              let jsonString=JSON.stringify(temp);
              that.sendMessage(jsonString)     
              var posx=[130,30];
              var posy=[50,90];
              var chang=[60,60];
              var kuan=[40,40];
              var tag=['red','green']
              var text=['严重龋坏','普通龋坏']
          var imgPool = [
            {
                "imgSrc":that.data.toothProblemsList[tag[0]],
                "x": 0,
                "y": 0,
                "width": chang[0]/scale,
                "height": kuan[0]/scale
            },
            {
                "imgSrc":that.data.toothProblemsList[tag[1]],
                "x": (posx[1]-chang[1]/2-20)/scale,
                "y": (posy[1]-kuan[1])/scale,
                "width": 100/scale,
                "height": 100/scale
            }
        ]

        //多张图像绘制
        var promise_list = [] 
        imgPool.forEach((item)=>{
          promise_list.push(canvas.createImage())
        })
        var imgpos=0;
        var loadimg=0;
        imgPool.forEach((item)=>{
          promise_list[loadimg].src=item.imgSrc
          promise_list[loadimg].onload=function(){
              imgpos++;
              console.log(imgpos)
              if(imgpos==imgPool.length)
              {
                imgpos=0;
               for(imgpos;imgpos<imgPool.length;++imgpos)
               {
                ctx.drawImage(
                  promise_list[imgpos],
                  imgPool[imgpos].x,
                  imgPool[imgpos].y,
                  imgPool[imgpos].width,
                  imgPool[imgpos].height)
               }
              }
          }
          loadimg++;
        })
            that.setData({
              exam:'none'
               })
               wx.hideLoading()
            
              },500)
            })
  },
  sendMessage(data){
    wx.showLoading({
      title: '请稍后',
    })
    var that=this;
    let SocketTask;
    SocketTask=wx.connectSocket({
        // url: 'wss://www.aistomatology.cn/ws',
        url: 'ws://127.0.0.1:8080',
   })
    SocketTask.onOpen((res) => {
      SocketTask.send({
        data: data,
        success: function (res) {
          console.log(res, '发送成功了')
        },
        fail: function (err) {
          console.log(err, '发送失败了')
        }
      })
    })
    SocketTask.onMessage((res)=>{
      let msg=res.data;
      console.log("接收数据:"+msg)
      const decodeStr=JSON.parse(msg)
      console.log("接收数据:"+decodeStr)
      that.setData({
        text:decodeStr
      })
    })
    SocketTask.onClose((res)=>{
      that.setData({
        jgWhetherOpen:0
      })
    })
  },
  
})